package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.query.QueryEmpEvaluateRecordReqBo;
import cn.cqie.frame.bo.query.QueryStudentEvaluateRecordBo;
import cn.cqie.frame.dto.UserContext;
import cn.cqie.frame.entity.*;
import cn.cqie.frame.mapper.DeptMapper;
import cn.cqie.frame.mapper.EvaluateRecordMapper;
import cn.cqie.frame.service.*;
import cn.cqie.frame.utils.AssertUtil;
import cn.cqie.frame.utils.DateUtils;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 评教记录 服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-18
 */
@Service
public class EvaluateRecordServiceImpl extends ServiceImpl<EvaluateRecordMapper, EvaluateRecord> implements EvaluateRecordService {
    @Autowired
    private ClazzCourseTeacherService clazzCourseTeacherService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private EvaluateResultService evaluateResultService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private EmpService empService;
    @Autowired
    private EvaluateRecordService evaluateRecordService;
    @Autowired
    private EvaluateTaskService evaluateTaskService;
    @Autowired
    private EvaluateTargetService evaluateTargetService;
    @Autowired
    private EvaluateTargetOptionService evaluateTargetOptionService;
    @Autowired
    private EvaluateTypeService evaluateTypeService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private StudentService studentService;
    /**
     * 学生评价
     * @param bo
     * @return
     */
    @Override
    public List<StudentEvaluateRecordVo> listPagedStudentEvaluate(QueryStudentEvaluateRecordBo bo) {
        MyPageHelper.startPage(bo);
        QueryWrapper<ClazzCourseTeacher> qw = new QueryWrapper<>();
        qw.inSql("clazzId", "select clazzId from student where userId=" + bo.getSysUserId());
        List<ClazzCourseTeacher> clazzCourseTeachers = clazzCourseTeacherService.list(qw);
        //3.生成返回值
        //映射准备-班级
        List<Clazz> allClazz = new ArrayList<>();
        List<Long> clazzIdRange = clazzCourseTeachers.stream()
                .filter(x -> x.getClazzId() != null)
                .map(x -> x.getClazzId())
                .distinct()
                .collect(Collectors.toList());
        if (clazzIdRange.size() > 0) {
            QueryWrapper<Clazz> qwClazz = new QueryWrapper<>();
            qwClazz.in("id", clazzIdRange);
            allClazz = clazzService.list(qwClazz);
        }
        //映射准备-课程
        List<Course> allCourse = new ArrayList<>();
        List<Long> courseIdRange = clazzCourseTeachers.stream()
                .filter(x -> x.getCourseId() != null)
                .map(x -> x.getCourseId())
                .distinct()
                .collect(Collectors.toList());
        if (courseIdRange.size() > 0) {
            QueryWrapper<Course> qwCourse = new QueryWrapper<>();
            qwCourse.in("id", courseIdRange);
            allCourse = courseService.list(qwCourse);
        }
        //映射准备-教师
        List<Emp> allTeacher = new ArrayList<>();
        List<Long> teacherIdRange = clazzCourseTeachers.stream()
                .filter(x -> x.getTeacherId() != null)
                .map(x -> x.getTeacherId())
                .distinct()
                .collect(Collectors.toList());
        if (teacherIdRange.size() > 0) {
            QueryWrapper<Emp> qwTeacher = new QueryWrapper<>();
            qwTeacher.in("id", teacherIdRange);
            allTeacher = empService.list(qwTeacher);
        }
        //映射准备-评价记录
        List<EvaluateRecord> allEvaluateRecord = new ArrayList<>();
        if (bo.getEvaluateTaskId() != null && teacherIdRange.size() > 0) {
            QueryWrapper<EvaluateRecord> qwEvaluateRecord = new QueryWrapper<>();
            qwEvaluateRecord.in("teacherId", teacherIdRange);
            qwEvaluateRecord.eq("evaluateTaskId", bo.getEvaluateTaskId());
            //学生评价
            qwEvaluateRecord.eq("evaluateTypeId", 1L);
            qwEvaluateRecord.eq("sysUserId", bo.getSysUserId());
            allEvaluateRecord = evaluateRecordService.list(qwEvaluateRecord);
        }
        //映射准备-评价任务是否能评价
        Boolean canEvaluate = false;
        System.out.println(bo.getEvaluateTaskId());
        if (bo.getEvaluateTaskId() != null) {
            EvaluateTask evaluateTask = evaluateTaskService.getById(bo.getEvaluateTaskId());
            Date now = new Date();
            if (evaluateTask != null) {
                canEvaluate = now.compareTo(evaluateTask.getStartDate()) >= 0 && now.compareTo(evaluateTask.getEndDate()) < 0 && evaluateTask.getStatus().equals(0);
            }
        }
        //映射
        List<StudentEvaluateRecordVo> list = new ArrayList<>();
        BeanUtils.copyProperties(clazzCourseTeachers, list);
        for (ClazzCourseTeacher mo : clazzCourseTeachers) {
            StudentEvaluateRecordVo vo = new StudentEvaluateRecordVo();
            System.out.println(mo);
            BeanUtils.copyProperties(mo, vo);
            //映射--班级
            vo.setClazzName(mapClazzNameById(mo.getClazzId(), allClazz));
            //映射--课程
            vo.setCourseName(mapCourseNameById(mo.getCourseId(), allCourse));
            //映射--教师
            vo.setTeacherName(mapTeacherNameById(mo.getTeacherId(), allTeacher));
            //映射--操作+选中备选项
            List<EvaluateRecord> findEvaluateRecordList = allEvaluateRecord.stream()
                    .filter(x -> x.getClazzId().equals(mo.getClazzId())
                            && x.getCourseId().equals(mo.getCourseId())
                            && x.getTeacherId().equals(mo.getTeacherId()))
                    .collect(Collectors.toList());
            if (findEvaluateRecordList.size() > 0) {
                vo.setOp(2);
            } else {
                vo.setOp(canEvaluate ? 1 : 0);
            }
            vo.setOptionIdList(findEvaluateRecordList.stream().map(EvaluateRecord::getEvaluateTargetOptionId).collect(Collectors.toList()));
            list.add(vo);
        }
        return list;
    }
    /**
     * 根据Id映射名称--班级
     */
    private String mapClazzNameById(Long id, List<Clazz> all) {
        if (id == null) {
            return null;
        }
        Clazz find = all.stream().filter(x -> x.getId().equals(id)).findFirst().orElse(null);
        if (find == null) {
            return null;
        }
        return find.getName();
    }

    /**
     * 根据Id映射名称--课程
     */
    private String mapCourseNameById(Long id, List<Course> all) {
        if (id == null) {
            return null;
        }
        Course find = all.stream().filter(x -> x.getId().equals(id)).findFirst().orElse(null);
        if (find == null) {
            return null;
        }
        return find.getName();
    }

    /**
     * 根据Id映射名称--教师
     */
    private String mapTeacherNameById(Long id, List<Emp> all) {
        if (id == null) {
            return null;
        }
        Emp find = all.stream().filter(x -> x.getId().equals(id)).findFirst().orElse(null);
        if (find == null) {
            return null;
        }
        return find.getName();
    }

    /**
     * 获取评价任务备选项
     * @return
     */
    @Override
    public List<EvaluateTask> listAllEvaluateTask() {
       return evaluateTaskService.list();

    }

    /**
     *
     * @param evaluateTypeId
     * @return
     */
    @Override
    public List<EvaluateTargetTreeData> listTreeEvaluateTarget(Long evaluateTypeId) {
        List<EvaluateTargetTreeData> ret = new ArrayList<>();
        //评价指标--指定评教类型
        QueryWrapper<EvaluateTarget> qwEvaluateTarget = new QueryWrapper<>();
        qwEvaluateTarget.eq("evaluateTypeId", evaluateTypeId);
        List<EvaluateTarget> allEvaluateTarget = evaluateTargetService.list(qwEvaluateTarget);
        //评价指标的备选项--指定评教类型
        QueryWrapper<EvaluateTargetOption> qwEvaluateTargetOption = new QueryWrapper<>();
        qwEvaluateTargetOption.inSql("evaluateTargetId", "select id from evaluate_target where evaluateTypeId=" + evaluateTypeId);
        List<EvaluateTargetOption> allEvaluateTargetOption = evaluateTargetOptionService.list(qwEvaluateTargetOption);
        //评价类型
        EvaluateType evaluateType = evaluateTypeService.getById(evaluateTypeId);
        //一级指标
        List<EvaluateTarget> first = allEvaluateTarget.stream().filter(x -> x.getParentId() == null).sorted(Comparator.comparing(EvaluateTarget::getSortCode)).collect(Collectors.toList());
        first.forEach(mo -> {
            int index = first.indexOf(mo) + 1;
            addSelfAndChildren(index + ".", mo, ret,allEvaluateTarget, allEvaluateTargetOption, evaluateType);
        });
        return ret;
    }


    /**
     *
     * @param pref
     * @param mo
     * @param ret
     * @param allEvaluateTarget
     * @param allEvaluateTargetOption
     * @param evaluateType
     */
    private void addSelfAndChildren(String pref, EvaluateTarget mo, List<EvaluateTargetTreeData> ret, List<EvaluateTarget> allEvaluateTarget, List<EvaluateTargetOption> allEvaluateTargetOption, EvaluateType evaluateType) {
        EvaluateTargetTreeData vo = new EvaluateTargetTreeData();
        BeanUtils.copyProperties(mo, vo);
        vo.setName(pref + vo.getName());
        //映射--评价类型名称
        vo.setEvaluateTypeName(evaluateType.getName());
        //映射--备选项列表
        List<EvaluateTargetOption> details = allEvaluateTargetOption.stream().filter(x -> x.getEvaluateTargetId().equals(mo.getId())).collect(Collectors.toList());
        vo.setEvaluateTargetOptionList(details);
        ret.add(vo);
        //子女
        List<EvaluateTarget> children = allEvaluateTarget.stream()
                .filter(x -> x.getParentId() != null && x.getParentId().equals(mo.getId()))
                .sorted(Comparator.comparing(EvaluateTarget::getSortCode))
                .collect(Collectors.toList());
        for (EvaluateTarget child : children) {
            int index = children.indexOf(child) + 1;
            addSelfAndChildren(pref + index + ".", child,ret, allEvaluateTarget, allEvaluateTargetOption, evaluateType);
        }
    }
    /**
     * 保存评价记录
     */
    @Override
    public Boolean saveRecord(List<EvaluateRecord> pos) {
        AssertUtil.isFalse(pos == null&&pos.size()<0,"没有数据需要保存");
        LocalDateTime sysTime = DateUtils.getLocalDateTime();
        pos.forEach(po -> {
            po.setEvaluateTime(sysTime);
        });
        boolean b = evaluateRecordService.saveBatch(pos);
        return b;
    }

    /**
     * 确定登录员工
     * @param sysUserId
     * @return
     */
    public Emp getLoginEmp(Integer sysUserId) {
        LambdaQueryWrapper<Emp> lqw = new LambdaQueryWrapper<Emp>();
        lqw.eq(Emp::getUserId,sysUserId);
        return empService.list(lqw).stream().findFirst().orElse(null);
    }

    /**
     * 确定登录用户担任角色
     * @param sysUserId
     * @return
     */
    public List<Long> getLoginRoleIdList(Integer sysUserId) {
        QueryWrapper<UserRole> qw=new QueryWrapper<>();
        qw.eq("userId", sysUserId);
        List<UserRole> allUserRole = userRoleService.list(qw);
        return allUserRole.stream()
                .map(UserRole::getRoleId)
                .collect(Collectors.toList());
    }

    /**
     * 员工评价
     * @param qo
     * @return
     */
    @Override
    public List<EmpEvaluateRecordVo> listPagedEmpEvaluate(QueryEmpEvaluateRecordReqBo qo) {
        MyPageHelper.startPage(qo);
        List<Dept> allDept= deptMapper.listDept();
        System.out.println(allDept);
        Emp loginEmp=getLoginEmp(qo.getSysUserId());
        List<Long> roleIdList=getLoginRoleIdList(qo.getSysUserId());
        //1.查询条件
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        //确定登录员工的评价范围
        List<Long> evaluateRange= getEmpEvaluateDeptRange(loginEmp,roleIdList,allDept);
        if(evaluateRange.size()>0) {
            qw.in("deptId", evaluateRange);
        }else{
            //登录员工的部门未知
            qw.eq("deptId",0);
        }
        List<Emp> empList = empService.list(qw);
        //3.生成返回值
        //映射准备-评价记录
        List<Long> teacherIdRange = empList.stream().map(Emp::getId).collect(Collectors.toList());

        List<EvaluateRecord> allEvaluateRecord = new ArrayList<>();
        if (qo.getEvaluateTaskId() != null && teacherIdRange.size() > 0) {
            QueryWrapper<EvaluateRecord> qwEvaluateRecord = new QueryWrapper<>();
            qwEvaluateRecord.in("teacherId", teacherIdRange);
            qwEvaluateRecord.eq("evaluateTaskId", qo.getEvaluateTaskId());
            qwEvaluateRecord.in("evaluateTypeId", new Long[]{2L,3L,4L,5L});
            qwEvaluateRecord.eq("sysUserId", qo.getSysUserId());
            allEvaluateRecord = evaluateRecordService.list(qwEvaluateRecord);
        }
        //映射准备-评价任务是否能评价
        Boolean canEvaluate = false;
        if (qo.getEvaluateTaskId() != null) {
            EvaluateTask evaluateTask = evaluateTaskService.getById(qo.getEvaluateTaskId());
            Date now = new Date();
            if (evaluateTask != null) {
                canEvaluate = now.compareTo(evaluateTask.getStartDate()) >= 0 && now.compareTo(evaluateTask.getEndDate()) < 0 && evaluateTask.getStatus().equals(0);
            }
        }
        List<EmpEvaluateRecordVo> list = new ArrayList<>();
        for (Emp mo : empList ) {
            EmpEvaluateRecordVo vo = new EmpEvaluateRecordVo();
            BeanUtils.copyProperties(mo, vo);
            //映射--部门
            if(mo.getDeptId()!=null) {
                Dept dept = allDept.stream().filter(x -> x.getId().equals(vo.getDeptId())).findFirst().orElse(null);
                if(dept!=null) {
                    vo.setDeptName(dept.getName());
                }
            }
            //映射--操作+选中备选项
            List<EvaluateRecord> find;
            //自评
            if(mo.getId().equals(loginEmp.getId())&&roleIdList.contains(4L)){
                find = allEvaluateRecord.stream().filter(x -> x.getEvaluateTypeId().equals(2L) && x.getTeacherId().equals(mo.getId())).collect(Collectors.toList());
                vo.setOp2((find.size() > 0)?2:(canEvaluate ? 1 : 0));
                vo.setOptionIdList2(find.stream().map(EvaluateRecord::getEvaluateTargetOptionId).collect(Collectors.toList()));
            }
            //同行评教
            if(mo.getId().equals(loginEmp.getId())==false && roleIdList.contains(4L)){
                find = allEvaluateRecord.stream().filter(x -> x.getEvaluateTypeId().equals(3L) && x.getTeacherId().equals(mo.getId())).collect(Collectors.toList());
                vo.setOp3((find.size() > 0)?2:(canEvaluate ? 1 : 0));
                vo.setOptionIdList3(find.stream().map(EvaluateRecord::getEvaluateTargetOptionId).collect(Collectors.toList()));
            }
            //教研室评教
            if(mo.getId().equals(loginEmp.getId())==false && roleIdList.contains(3L)) {
                find = allEvaluateRecord.stream().filter(x -> x.getEvaluateTypeId().equals(4L) && x.getTeacherId().equals(mo.getId())).collect(Collectors.toList());
                vo.setOp4((find.size() > 0) ? 2 : (canEvaluate ? 1 : 0));
                vo.setOptionIdList4(find.stream().map(EvaluateRecord::getEvaluateTargetOptionId).collect(Collectors.toList()));
            }
            //系部评教
            if(mo.getId().equals(loginEmp.getId())==false && roleIdList.contains(2L)) {
                find = allEvaluateRecord.stream().filter(x -> x.getEvaluateTypeId().equals(5L) && x.getTeacherId().equals(mo.getId())).collect(Collectors.toList());
                vo.setOp5((find.size() > 0) ? 2 : (canEvaluate ? 1 : 0));
                vo.setOptionIdList5(find.stream().map(EvaluateRecord::getEvaluateTargetOptionId).collect(Collectors.toList()));
            }
            list.add(vo);
        }

        return list;
    }



    /**
     * 获取员工评价的部门范围
     */
    private List<Long> getEmpEvaluateDeptRange(Emp loginEmp, List<Long> roleIdList, List<Dept> allDept) {
        List<Long> ret=new ArrayList<>();
        if(loginEmp!=null && loginEmp.getDeptId()!=null){
            Long roleId=2L;
            //系主任--所在部门及其子部门
            if(roleIdList.contains(roleId)) {
                //递归
                generateDeptIdRange(loginEmp.getDeptId(), ret, allDept);
            }else{ //教研室主任 或 教师--所在部门
                ret.add(loginEmp.getDeptId());
            }
        }
        return ret;
    }
    /**
     * 递归方法:添加当前部门Id及子部门的Id到deptIdRange
     */
    private void generateDeptIdRange(Long selfId,List<Long> deptIdRange,List<Dept> all){
        //自己
        deptIdRange.add(selfId);
        //子女
        List<Dept> children = all.stream().filter(x -> x.getParentId() != null && x.getParentId().equals(selfId)).collect(Collectors.toList());
        for(Dept dept:children){
            generateDeptIdRange(dept.getId(),deptIdRange,all);
        }
    }
    /**
     * 应评人数
     */
    private int getRequirePersonCount(Long evaluateTypeId, Long teacherId, Long teacherDeptId) {
        int ret = 0;
        QueryWrapper<Emp> qwEmp = new QueryWrapper<>();
        //学生评教
        if (evaluateTypeId == 1L) {
            QueryWrapper<ClazzCourseTeacher> qwClazzCourseTeacher = new QueryWrapper<>();
            qwClazzCourseTeacher.eq("teacherId", teacherId);
            List<ClazzCourseTeacher> clazzCourseTeacherList = clazzCourseTeacherService.list(qwClazzCourseTeacher);
            for (ClazzCourseTeacher clazzCourseTeacher : clazzCourseTeacherList) {
                QueryWrapper<Student> qwStudent = new QueryWrapper<>();
                qwStudent.eq("clazzId", clazzCourseTeacher.getClazzId());
                List<Student> studentList = studentService.list(qwStudent);
                ret += studentList.size();
            }
            //教师自评
        } else if (evaluateTypeId == 2L) {
            //教师
            qwEmp.inSql("userId", "select userId from user_role where roleId=4");
            //自己
            qwEmp.eq("id", teacherId);
            ret = empService.list(qwEmp).size();
            //同行评教
        } else if (evaluateTypeId == 3L) {
            qwEmp.eq("deptId", teacherDeptId);//同一部门
            qwEmp.inSql("userId", "select userId from user_role where roleId=4");//教师
            qwEmp.ne("id", teacherId);//排除自己
            ret = empService.list(qwEmp).size();
        } else if (evaluateTypeId == 4L) {//教研室评教
            qwEmp.eq("deptId", teacherDeptId);//同一部门
            qwEmp.inSql("userId", "select userId from user_role where roleId=3");//教研室主任
            qwEmp.ne("id", teacherId);//排除自己
            ret = empService.list(qwEmp).size();
        } else if (evaluateTypeId == 5L) {//系主任评教
            Dept dept = deptService.getById(teacherDeptId);
            qwEmp.eq("deptId", dept.getParentId());
            qwEmp.inSql("userId", "select userId from user_role where roleId=2");//系主任
            qwEmp.ne("id", teacherId);//排除自己
            ret = empService.list(qwEmp).size();
        }

        return ret;
    }

    /**
     * 计算分值
     * @param taskId
     */
    @Override
    public void calculateScore(Long taskId) {
        QueryWrapper<EvaluateRecord> qwRecord = new QueryWrapper<>();
        qwRecord.eq("evaluateTypeId", taskId);
        List<EvaluateRecord> listRecord = evaluateRecordService.list(qwRecord);
        //数据准备--选中备选项
        List<EvaluateTargetOption> allOption = new ArrayList<>();
        List<Long> idRangeForOption = listRecord.stream()
                .filter(x -> x.getEvaluateTargetOptionId() != null)
                .map(x -> x.getEvaluateTargetOptionId())
                .collect(Collectors.toList());
        if (idRangeForOption.size() > 0) {
            QueryWrapper<EvaluateTargetOption> qw = new QueryWrapper<>();
            qw.in("id", idRangeForOption);
            allOption = evaluateTargetOptionService.list(qw);
        }
        //数据准备--指标
        List<EvaluateTarget> allTarget = evaluateTargetService.list();
        //数据准备--教师
        List<Emp> allTeacher=new ArrayList<>();
        List<Long> idRangeForTeacher = listRecord.stream()
                .filter(x -> x.getTeacherId() != null)
                .map(EvaluateRecord::getTeacherId)
                .collect(Collectors.toList());
        if(idRangeForTeacher.size()>0){
            QueryWrapper<Emp> qw=new QueryWrapper<>();
            qw.in("id",idRangeForTeacher);
            allTeacher=empService.list(qw);
        }
        Date now=new Date();
        List<EvaluateResult> evaluateResultList=new ArrayList<>();
        List<EvaluateType> listType = evaluateTypeService.list();
        for (EvaluateType type : listType) {
            for(Emp teacher:allTeacher) {
                int personCount = 0;//参评人数
                int courseCount = 0;//科目数量
                BigDecimal totalScore = new BigDecimal(0);
                Long userId=-1L;
                List<EvaluateRecord> findRecord = listRecord.stream()
                        .filter(x -> x.getEvaluateTypeId().equals(type.getId())
                                && x.getTeacherId().equals(teacher.getId()))
                        .sorted(Comparator.comparing(EvaluateRecord::getSysUserId))
                        .collect(Collectors.toList());
                //科目数
                courseCount = findRecord.stream()
                        .filter(x -> x.getCourseId() != null)
                        .map(x -> x.getCourseId())
                        .distinct()
                        .collect(Collectors.toList())
                        .size();
                for (EvaluateRecord record : findRecord) {
                    //参评人
                    if(!record.getSysUserId().equals(userId)) {
                        userId=record.getSysUserId();
                        personCount += 1;
                    }
                    //分值
                    EvaluateTargetOption option = allOption.stream()
                            .filter(x -> x.getId().equals(record.getEvaluateTargetOptionId()))
                            .findFirst().orElse(null);
                    if (option == null) {
                        continue;
                    }
                    BigDecimal weight = getWeight(record.getEvaluateTargetId(), allTarget);
                    totalScore.add(option.getScore().multiply(weight));
                }
                //保存结果
                EvaluateResult mo=new EvaluateResult();
                mo.setEvaluateTaskId(taskId);
                mo.setTeacherId(teacher.getId());
                mo.setEvaluateTypeId(type.getId());
                mo.setScore(totalScore.divide(new BigDecimal(personCount==0?1:personCount)).divide(new BigDecimal(courseCount==0?1:courseCount)));
                mo.setAttendPersonCount(personCount);
                mo.setCalculateTime(now);
                evaluateResultList.add(mo);
            }
        }
        if(evaluateResultList.size()>0){
            evaluateResultService.saveBatch(evaluateResultList);
        }
    }

    /**
     * 得到自己及上级指标的权重乘积--递归方法
     * @param targetId
     * @param all
     * @return
     */
    private BigDecimal getWeight(Long targetId, List<EvaluateTarget> all) {
        if(targetId==null){
            return new BigDecimal(1);
        }
        EvaluateTarget self = all.stream()
                .filter(x -> x.getId().equals(targetId))
                .findFirst().orElse(null);

        if(self==null){
            return new BigDecimal(1);
        }
        //权重归一化
        List<EvaluateTarget> brothers=new ArrayList<>();
        if(self.getParentId()!=null) {
            brothers = all.stream()
                    .filter(x -> x.getParentId().equals(self.getParentId()))
                    .collect(Collectors.toList());
        }else{
            brothers = all.stream()
                    .filter(x -> x.getParentId()==null && x.getEvaluateTypeId().equals(self.getEvaluateTypeId()))
                    .collect(Collectors.toList());
        }
        BigDecimal sum=new BigDecimal(0);
        for(EvaluateTarget target:brothers){
            sum.add(target.getWeight());
        }
        if(sum.longValue()==0L){
            sum=new BigDecimal(1);
        }
        return self.getWeight().divide(sum).multiply(getWeight(self.getParentId(),all));
    }

}

